﻿package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.model.bean.GetUserOnlineInfo;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.GetUserInfoOnlineParser;

/**
 * 获取在线用户身份
 * 
 * @author yanghuawei
 */
public class GetOnlineUserInfoModel implements HttpRequestListener {
    private final String logTag = "GetOnlineUserInfoModel";
    private HttpRequest httpRequest;
    private OnGetOnlineUserInfoListener listener;
    private Context context;

    public GetOnlineUserInfoModel(Context context, OnGetOnlineUserInfoListener listener) {
        this.listener = listener;
        this.context = context.getApplicationContext();
    }

    public void loadData() {
        httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), setParams(),
                        this);
        httpRequest.setDataParseAdapter(new GetUserInfoOnlineParser());
        MyTask.runInBackground(httpRequest, false);
    }

    @Override
    public void action(int actionCode, Object object) {
        switch (actionCode) {
            case EventTypeConstants.EVENT_SUCCESS:// 读取数据 写处理逻辑
                GetUserOnlineInfo info = (GetUserOnlineInfo) object;
                if (info != null && "200".equals(info.getServerState())) {
                    listener.OnFinish(GetUserOnlineInfo.RESULT_SUCCESS, info);
                    Log4an.i(logTag, info.toString());
                } else {
                    listener.OnFinish(GetUserOnlineInfo.RESULT_FAIL, null);
                }
                break;
            default:
                listener.OnFinish(GetUserOnlineInfo.RESULT_FAIL_NET, null);
                break;
        }
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        String key = "c56cf32e9a52a265ae47cd50570266cc";
        long timestamp = System.currentTimeMillis();
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = Util.MD5(md5_key_sb.toString());
        params.add(new BasicNameValuePair("key", md5_key));// md5
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳
        // params.add(new BasicNameValuePair("imei", ConfigVersion.IMEI));
        return params;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getGetAccountStateOnlineUrl();
    }

    public interface OnGetOnlineUserInfoListener {
        /**
         * 在线获取用户信息
         * 
         * @param type -1，网络失败，0，失败，1成功
         * @param info
         */
        public void OnFinish(int type, GetUserOnlineInfo info);
    }
}
